package zunge.twidel.typo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TweetCleaner {
	
	/**
	 * Remove all "#tag",  remove all "@username", replace all URLs using "URL"
	 */
	public static String remove(String text){
		String[] tokens = text.split(" ");
		List<String> ret = new ArrayList<String>();
		for(int i = 0;i < tokens.length;++i){
			if(tokens[i].charAt(0)=='#'){continue;}
			else if(tokens[i].charAt(0)=='@'){continue;}
			else if(tokens[i].startsWith("http://")){ret.add("URL");}
			else{ret.add(tokens[i]);}
		}
		if(ret.size()==0){return null;}
		else{return combine(ret);}
	}
	
	/**
	 * Remove all "#",  replace all "@username" using "USER", replace all URLs using "URL", 
	 */
	public static String replace(String text){
		String[] tokens = text.split(" ");
		List<String> ret = new ArrayList<String>();
		for(int i = 0;i < tokens.length;++i){
			if(tokens[i].charAt(0)=='#' && tokens[i].length()>1){ret.add(tokens[i].substring(1));}
			else if(tokens[i].charAt(0)=='@' && tokens[i].length()>1){ret.add("USER");}
			else if(tokens[i].startsWith("http://")){ret.add("URL");}
			else{ret.add(tokens[i]);}
		}
		if(ret.size()==0){return null;}
		else{return combine(ret);}
	}
	
	
	/**
	 * Alphabetic Reorder
	 */
	public static String abReorder(String text){
		List<String> frags = new ArrayList<String>();
		for(String s:text.split(" ")){frags.add(s);}
		Collections.sort(frags);
		return combine(frags);
	}
	
	static String combine(List<String> frags){
		StringBuffer sb = new StringBuffer();
		for(String frag:frags){
			sb.append(" "+frag);
		}
		String ret = new String(sb);
		return ret.substring(1);
	}
}
